package com.hsy.managemoney.modules.main;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import com.deparse.dpframe.ui.fragment.DPBaseFragment;
import com.deparse.dpframe.ui.view.DPTabItemContainer;
import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.managemoney.R;
import com.hsy.managemoney.modules.common.BaseHaveActionBarActivity;
import com.hsy.managemoney.modules.common.BaseNoActionBarActivity;
import com.hsy.managemoney.modules.main.actionbar.ActionBarManageMoney;
import com.hsy.managemoney.modules.main.actionbar.ActionBarMy;
import com.hsy.managemoney.modules.main.actionbar.ActionBarPrivilege;
import com.hsy.managemoney.modules.main.actionbar.ActionBarRecommend;
import com.hsy.managemoney.modules.main.actionbar.IActionBar;
import com.hsy.managemoney.modules.managemoney.FragmentManageMoney;
import com.hsy.managemoney.modules.my.FragmentMy;
import com.hsy.managemoney.modules.privilege.FragmentPrivilege;
import com.hsy.managemoney.modules.recommend.FragmentRecommend;
import com.hsy.managemoney.modules.utils.CommonUtils;

import java.lang.reflect.InvocationTargetException;

/**
 * Created by Administrator on 2016/10/11.
 */
public class MainActivity extends BaseHaveActionBarActivity {
    private DPTabItemContainer tab;
    private Class<?>[] fragments = {
            FragmentRecommend.class,
            FragmentManageMoney.class,
            FragmentPrivilege.class,
            FragmentMy.class
    };
    private Class<?>[] actionBars = {
            ActionBarRecommend.class,
            ActionBarManageMoney.class,
            ActionBarPrivilege.class,
            ActionBarMy.class
    };
    private View currentActionBar;
    private Fragment currentFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getActionBarContainer().setVisibility(View.GONE);
        CommonUtils.setToken(this, "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJwMnAuZGV2LmhzeWRldi5jb21cL2FwaS5waHAiLCJhdWQiOiIqIiwiaWF0IjoxNDc4MDUwOTIyLCJuYmYiOjE0NzgwNTA5MjIsImV4cCI6MTQ4MDY0MjkyMiwidWlkIjoiMSIsInRva2VuIjoiRTU4RUE3OTJERUM0NTNFNEFDRjBENEQ1RjE2M0EwOTQifQ.U9HaUQwSu9m4gNukcGZB-Q5MTWquD6OFrDATWWX4Pa0");
        tab = F(R.id.tabs);
        tab.setOnTabChangeListener(new DPTabItemContainer.OnTabChangeListener() {
            @Override
            public void beforeTabChange(int index, View tabWidget) {
            }

            @Override
            public void afterTabChange(int index, View tabWidget) {
                try {
                    if (0 != index) {
                        getActionBarContainer().setVisibility(View.VISIBLE);
                    } else {
                        getActionBarContainer().setVisibility(View.GONE);
                    }
                    View actionBar = (View) actionBars[index].getConstructor(Context.class).newInstance(MainActivity.this);
                    setActionBar(actionBar);
                    currentActionBar = actionBar;
                    if (actionBar instanceof IActionBar) {
                        ((IActionBar) actionBar).setActivity(MainActivity.this);
                    }

                    currentFragment = (Fragment) fragments[index].newInstance();
                    if (currentFragment instanceof DPBaseFragment) {
                        ((DPBaseFragment) currentFragment).setAttchedActivity(MainActivity.this);
                    }
                    replaceFragment(R.id.container, currentFragment);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        });
        tab.setCurrentTab(0);
    }
}
